package de.clapp.musicbox.viewpages;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import de.clapp.R;
import de.clapp.data.MusicItem;
import de.clapp.musicbox.MusicFragment;
import de.clapp.musicbox.MusicModel;
import de.clapp.musicbox.MusicVoteThread;
import de.clapp.musicbox.listAdapter.AlbumListAdapter;
import de.clapp.musicbox.listAdapter.InterpretListAdapter;
import de.clapp.musicbox.listAdapter.TitleListAdapter;
import de.clapp.musicbox.loader.MusicVoteRequestLoader;

@SuppressLint("ValidFragment")
public class TitleMusicFragmentList extends MusicFragmentList implements OnQueryTextListener, LoaderCallbacks<Object>
{
    private SearchView mySearchView;

    private TitleListAdapter adapter;

    public TitleMusicFragmentList()
    {
        super("Titel");
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // this.getActivity().getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        // this.getActivity().setProgressBarIndeterminateVisibility(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // super.onCreateView(inflater, container, savedInstanceState);

        setHasOptionsMenu(true);
        this.adapter = new TitleListAdapter(inflater.getContext(), this);
        this.setListAdapter(this.adapter);
        
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ListView listView = (ListView) view.findViewById(android.R.id.list);
        listView.setOnKeyListener(new OnKeyListener()
        {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if (keyCode == KeyEvent.KEYCODE_BACK)
                {
                    MusicFragment.myViewPager.setCurrentItem(MusicFragment.myViewPager.getCurrentItem() - 1, true);
                    return true;
                }
                return false;
            }
        });

        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int index, long id)
    {
        this.adapter.setSelectedArtist(index);
        String artist = InterpretListAdapter.SelectedArtist;
        String album = AlbumListAdapter.SelectedAlbum;
        String title = TitleListAdapter.SelectedTitle;

        MusicItem item = MusicModel.getI().getSong(artist, album, title);
        MusicVoteThread t = new MusicVoteThread(item);
        t.start();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (this.adapter.getCount() <= 0)
        {
            this.setListShown(false);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        MenuInflater menuInflater = getActivity().getMenuInflater();
        menuInflater.inflate(R.menu.menu_menucard, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        this.mySearchView = (SearchView) searchItem.getActionView();

        this.mySearchView.setIconifiedByDefault(false);
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        this.mySearchView.setOnQueryTextListener(this);
    }

    @Override
    public boolean onQueryTextChange(String constraint)
    {
        Filter filter = this.adapter.getFilter();
        filter.filter(constraint);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String arg0)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Loader<Object> onCreateLoader(int arg0, Bundle arg1)
    {
        String artist = InterpretListAdapter.SelectedArtist;
        String album = AlbumListAdapter.SelectedAlbum;
        String title = TitleListAdapter.SelectedTitle;

        MusicItem item = MusicModel.getI().getSong(artist, album, title);
        return new MusicVoteRequestLoader(this.getActivity(), item);
    }

    @Override
    public void onLoadFinished(Loader<Object> arg0, Object arg1)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLoaderReset(Loader<Object> arg0)
    {
        // TODO Auto-generated method stub

    }

}
